<?php

class Home extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();
		// load language to initialize	
		//$this->session->unset_userdata('lang');
		/*
		$lang = $this->session->userdata('lang');
		echo $this->session->userdata('ip_address');
		var_dump($lang); echo '<br/>'; */
		//$this->output->cache(3600);
		$this->template['module'] = 'news';
		$this->template['lang_file'] = 'news';

		// Load needed models, libraries, helpers and language files
		//echo "kkk"; exit();
		$this->load->module_model('news', 'news_model', 'news');
		$this->load->module_model('news', 'user_model', 'user');
		$this->load->module_model('news', 'language_model', 'language');
		
		$this->load->library('user_agent');
		//echo $lang['name'];
		/*
		if($lang == false)
		{
			//echo 'kkk';
			$language = $this->language->get_lang_id('vn');
			//var_dump($language);
			$lang = $language;
			$this->session->set_userdata('lang', $language);
		}*/
		//echo $lang['name'];
		//$this->load->module_language('news', 'general',$lang['name']);
		//$this->load->module_language('news', 'sidebar',$lang['name']);
		//$this->load->module_language('news', 'general');
		//$this->load->module_language('news', 'sidebar');
		//$this->load->library('securimage_library');
	}

	// Public methodse
	public function pages($url_title = null)
	{
		$this->load_language();
		if ($data['post'] = $this->news->get_page_by_url($url_title))
		{
			$this->_template['page']	= 'news/page';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}
	public function index($page = null)
	{
		// Load language
		$lang = $this->load_language();
		//var_dump($lang);
		$this->load->library('pagination');
		
		$this->load->module_language('home', 'general',$lang['name']);
		$this->load->module_language('home', 'sidebar',$lang['name']);
		$this->load->module_language('home', 'footer',$lang['name']);
		$config['total_rows'] = $this->news->get_posts_count($lang['id']);
		$config['per_page'] = $this->news->get_posts_per_page();
		$this->pagination->initialize($config);
			
		$pages_count = ceil($config['total_rows'] / $config['per_page']);
		$page = ($page == 0) ? 1 : $page;
		$offset = $config['per_page'] * ($page - 1);
		$sticky_post = $this->news->get_the_last_sticky_post($lang['id']);
		$lastpost = $this->news->get_the_last_post($lang['id']);	
		$data['lastpost'] = $lastpost;
		$data['sticky_post'] = $sticky_post;
		if ($data['posts'] = $this->news->get_posts($config['per_page'], $offset,$lang['id']))
		{
			if ($page > $pages_count)
			{
				redirect('news', 'refresh');
			}
		
			$data['posts_per_page'] = $this->news->get_posts_per_page();
			$data['posts_count'] = $this->news->get_posts_count();
			$data['pages_count'] = $pages_count;
			$data['current_page'] = $page;
			$data['next_page'] = $page + 1;
			$data['previous_page'] = $page - 1;
			$navigation['menuID'] = '1'; // HomePage
			$data['navigation'] = $navigation;
			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['created_date']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'home/home';
		}
		else
		{
			$this->_template['page']	= 'errors/no_posts';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}

	public function post($year = NULL, $month = NULL, $day = NULL, $url_title = NULL)
	{
		// Load language
		$this->load_language();
		
		$this->load->module_model('news', 'user_model', 'user');
			
		if ($data['post'] = $this->news->get_post_by_url($year, $month, $day, $url_title))
		{
			
			$data['post']['url'] = post_url($data['post']['url_title'], $data['post']['created_date']);
			$this->_template['page']	= 'news/single_post';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}

	public function archive($year = null, $month = null)
	{
		if ($data['posts'] = $this->blog->get_posts_by_date($year, $month))
		{
			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['date_posted']);
				$data['posts'][$key]['display_name'] = $this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'blog/archive';
		}
		else
		{
			$this->_template['page']	= 'errors/archive_no_posts';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}

	public function category($url_name = null)
	{		
		// Load language
		$this->load_language();
		//echo $url_name; exit();
		if ($data['posts'] = $this->news->get_posts_by_category($url_name))
		{
			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['created_date']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'news/archive';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function tags($tag_name = null)
	{
		$data['tag_name'] = $tag_name;
		
		if ($data['posts'] = $this->blog->get_posts_by_tags($tag_name))
		{
			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['date_posted']);
				$data['posts'][$key]['display_name'] = $this->users->get_user_display_name($post['author']);
			}
				
			$this->_template['page']	= 'blog/tags';
		}
		else
		{
			$this->_template['page']	= 'errors/tags_no_results';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}

	public function search()
	{
		// Load language
		$this->load_language();
		
		$data['search_term'] = $this->input->post('term', TRUE);
			
		if ($data['search_term'] != "")
		{
			if ($data['posts'] = $this->blog->get_posts_by_term($data['search_term']))
			{
				foreach ($data['posts'] as $key => $post)
				{
					$data['posts'][$key]['url'] = post_url($post['url_title'], $post['date_posted']);
					$data['posts'][$key]['display_name'] = $this->users->get_user_display_name($post['author']);
				}
					
				$this->_template['page']	= 'blog/search';
			}
			else
			{
				$this->_template['page']	= 'errors/search_no_results';
			}
				
			$this->system_library->load($this->_template['page'], $data);
		}
		else
		{
			redirect('blog', 'refresh');
		}
	}

	public function lang($lang = null)
	{
		if($lang == null)
		{
			return $lang = 'vn';
		}
		$language = $this->language->get_lang_id($lang);
		//print_r($language);
		$this->session->set_userdata('lang', $language);
		//redirect('', 'refresh');
		//for test session
		//$t = $this->session->userdata('lang');
		//print_r($t); //exit();
		//echo '<br/>';
		
		$this->load->library('pagination');
		$lang = $this->session->userdata('lang');
		
		$this->load->module_language('news', 'general',$lang['name']);
		$this->load->module_language('news', 'sidebar',$lang['name']);
		
		$config['total_rows'] = $this->news->get_posts_count($lang['id']);
		$config['per_page'] = $this->news->get_posts_per_page();
		$this->pagination->initialize($config);
			
		$pages_count = ceil($config['total_rows'] / $config['per_page']);
		$page = ($page == 0) ? 1 : $page;
		$offset = $config['per_page'] * ($page - 1);
			
		if ($data['posts'] = $this->news->get_posts($config['per_page'], $offset,$lang['id']))
		{
			if ($page > $pages_count)
			{
				redirect('news', 'refresh');
			}
		
			$data['posts_per_page'] = $this->news->get_posts_per_page();
			$data['posts_count'] = $this->news->get_posts_count();
			$data['pages_count'] = $pages_count;
			$data['current_page'] = $page;
			$data['next_page'] = $page + 1;
			$data['previous_page'] = $page - 1;

			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['created_date']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'news/posts';
		}
		else
		{
			$this->_template['page']	= 'errors/no_posts';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}
	/*
	 * private function to load language
	 */
	public function load_language()
	{

		$lang = $this->session->userdata('lang');
		
		//load language
		if($lang == false)
		{
			$language = $this->language->get_lang_id('vn');
			$lang = $language;
			$this->session->set_userdata('lang', $language);
		}
		//var_dump(strtolower($lang['name']));
		$this->load->module_language('news', 'general',$lang['name']);
		$this->load->module_language('news', 'sidebar',$lang['name']);
		return $lang;
	}
}

/* End of file blog.php */
/* Location: ./application/modules/blog/controllers/blog.php */